﻿using System;
using Framework.Remoting.Client;
using Framework.Remoting.Testing;
using Framework.Tests.Wcf.Client.ServiceDefinition;
using Xunit;

namespace Framework.Tests.Wcf.Client
{
    public class WcfIntegrationTest : IDisposable
    {
        private readonly IntegrationTest<IContract, Contract> integrationTest = new IntegrationTest<IContract, Contract>();

        [Fact]
        public void Simple_query_and_get_result()
        {
            Assert.Equal("pouet", integrationTest.Query(x => x.Get()).Name);
        }

        [Fact]
        public void Should_call_proc()
        {
            integrationTest.Query(x => x.JustDoIt());
        }

        [Fact]
        public void When_call_get_with_fault_exception_expected_fault_exception_of_exception_detail()
        {
            Assert.Throws<System.ServiceModel.FaultException<ExceptionDetail>>(() => integrationTest.Query(x => x.GetWithFaultContract()));
        }

        [Fact]
        public void When_call_get_with_exception_not_in_fault_contract_then_throw_exception_to_notify_dev()
        {
            Assert.Throws<MissingFaultContractDeclarationException>(() => integrationTest.Query(x => x.GetWithExceptionNotInFaultContract()));
        }

        [Fact]
        public void On_communication_exception_retry()
        {
            integrationTest.SimulateCommunicationProblem();
            Assert.Throws<WcfProxyException>(() =>
                {
                    integrationTest.Query(x => x.Get());
                });
        }

        public void Dispose()
        {
            integrationTest.Dispose();
        }
    }
}